package com.rangers.manage.email.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

import lombok.extern.slf4j.Slf4j;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Slf4j
@Configuration
public class SpringfoxConfig {
	@EnableSwagger2
	public static class SwaggerConfig {

		@Bean
		@Profile("!prod")
		public Docket apiDev() {
			// @formatter:off
			return new Docket(DocumentationType.SWAGGER_2).select()
					.apis(RequestHandlerSelectors.basePackage("com.rangers.manage.email.ctrl"))
					.paths(PathSelectors.any()).build().apiInfo(apiInfo());
			// @formatter:on
		}

		@Bean
		@Profile("prod")
		public Docket apiProd() {
			return new Docket(DocumentationType.SWAGGER_2).select().paths(PathSelectors.none()).build()
					.apiInfo(apiInfo());
		}

		private ApiInfo apiInfo() {
			String projectname = System.getProperty("user.dir");
			String pn = projectname.substring(projectname.lastIndexOf("/") + 1, projectname.length());
			log.info("当前项目名:[{}]", pn);
			// @formatter:off
			return new ApiInfoBuilder().title(pn + "(API)").description(pn + "-API 文档").version("v1.0").build();
			// @formatter:on
		}
	}
}
